package com.zys.background.common.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @author zys
 * @since 2022-11-12
 */
@Data
@ApiModel("统一返回结果")
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class Result<T> {

  @ApiModelProperty("返回状态码, 200 成功, 400 失败")
  private Integer code;

  @ApiModelProperty("返回提示信息")
  private String msg;

  @ApiModelProperty("返回的数据")
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  private T data;


  public static <T> Result<T> result(ResultEnum resultEnum) {
    return result(resultEnum, null);
  }

  public static <T> Result<T> result(ResultEnum resultEnum, T data) {
    return new Result<>(resultEnum.getCode(), resultEnum.getMsg(), data);
  }

  public static <T> Result<T> success(String msg, T data) {
    return new Result<>(200, msg, data);
  }

  public static <T> Result<T> success(String msg) {
    return success(msg, null);
  }

  public static <T> Result<T> fail() {
    return fail("访问异常");
  }

  public static <T> Result<T> fail(String msg) {
    return new Result<>(400, msg, null);
  }

  public static <T> Result<T> judge(boolean flag) {
    return judge(flag, "操作成功", "操作异常");
  }

  public static <T> Result<T> judge(boolean flag, String successMsg, String failMsg) {
    return judge(flag, successMsg, failMsg, null);
  }

  public static <T> Result<T> judge(boolean flag, String successMsg, String failMsg, T data) {
    return flag ? success(successMsg, data) : fail(failMsg);
  }
}
